import { GameRules, GameRulesTypeA } from '@/services/game/GameController';
import { ProForm, ProFormCheckbox, ProFormText } from '@ant-design/pro-components';
import { Card, Flex, message, Modal, Typography } from 'antd';
import React from 'react';
import services from '@/services/game';

/**
 * 积分晋级赛详细规则设置
 */

const { editGameRules } = services.GameController

/**
 * 编辑规则
 * @param fields
 */
const handleEdit = async (rulesId: string, fields: GameRulesTypeA) => {

  const hide = message.loading('正在保存');
  try {
    const { success, errorMessage } = await editGameRules({
      rulesId: rulesId,
      data: fields
    });
    hide();
    if (success) {
      message.success('保存成功');
      return true;
    } else {
      message.error(errorMessage);
      return false;
    }
  } catch (error) {
    hide();
    message.error('保存失败请重试！');
    return false;
  }
};
interface Props {
  data?: GameRules
  modalVisible: boolean;
  onCancel: (show: boolean) => void;
  onReloadAndRest?: () => void;
}
const GameEditRulesFormTypeA: React.FC<Props> = (props) => {
  const { data, modalVisible, onCancel, onReloadAndRest } = props;
  return (
    <Modal
      title="积分晋级赛规则设置"
      open={modalVisible}
      onCancel={() => onCancel(false)}
      footer={null}
      width="60%"
      height="60%"
      style={{ padding: 0, top: 10 }}
    >
      <ProForm<GameRulesTypeA>
        initialValues={JSON.parse('{}')}
        onFinish={async (values) => {
          const success = await handleEdit(data?.rulesId ?? '', values);
          if (success) {
            onReloadAndRest?.();
          }
        }}
      >

        <Card title='基础规则'>
          <Flex style={{ justifyContent: 'space-between' }}>
            <ProFormText
              width="md"
              initialValue={20}
              name="basicPoints"
              label="基础分"
              rules={[
                {
                  required: true,
                  message: '请输入基础分',
                },
              ]}
            />
            <ProFormText
              width="md"
              initialValue={120}
              name="promotePoints"
              label="晋级分"
              rules={[
                {
                  required: true,
                  message: '请输入晋级分',
                },
              ]}
            />
          </Flex>
        </Card>

        <Card title='排名规则'>
          <ProFormCheckbox.Group
            name="rankingCheck"
            initialValue={[1, 2, 3]}
            options={[
              {
                label: '比赛积分',
                value: 1,
              },
              {
                label: '比赛用时',
                value: 2,
              },
              {
                label: '净胜场',
                value: 3,
              },
            ]}
          />
        </Card>

        <Card title='积分规则'>
          <Typography.Text>得分=max(负方分*得分比例,10) 四舍五入取整10，胜方加得分值，负方扣得分值</Typography.Text>
          <ProFormText
            width="md"
            name="pointsRatio"
            label="得分比例"
            initialValue={0.5}
            rules={[
              {
                required: true,
                message: '请输入得分比例',
              },
            ]}
          />
        </Card>
        <Card title='弃权规则'>
          <ProFormText
            width="md"
            name="abstainTime"
            initialValue={50}
            label="比赛用时(分)"
            rules={[
              {
                required: true,
                message: '请输入用时(分)',
              },
            ]}
          />

        </Card>
        <Card title='淘汰规则'>
          <ProFormText
            width="md"
            initialValue={0}
            name="outPoints"
            label="积分"
            rules={[
              {
                required: true,
                message: '请输入积分',
              },
            ]}
          />
        </Card>

      </ProForm>

    </Modal >
  );
};

export default GameEditRulesFormTypeA;
